//
//  Zh_GoodsInformationViewController.m
//  ChangeMerchants
//
//  Created by zhangliwen on 2018/9/28.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "Zh_GoodsInformationViewController.h"
#import "GC_GoodsSegmentView.h"
#import "GC_HPBannerTableViewCell.h"
#import "ZH_GoodsInfoTableViewCell.h"
#import "ZH_GoodsCenterTableViewCell.h"
#import "ZH_GoodsInformationTableViewCell.h"
#import "ZH_GoodsEvaluationViewController.h"
#import "ZH_MallOrderViewModel.h"
#import "ZH_GoodsInfoView.h"
#import "ZH_MallViewModel.h"
@interface Zh_GoodsInformationViewController()<UITableViewDelegate, UITableViewDataSource, GC_GoodsSegmentDelegate>
///选择 View
@property (nonatomic, weak) GC_GoodsSegmentView *segmentView;
///当前选择 0：商品；1：详情  3：评价
@property (nonatomic, assign) NSInteger currentItem;
@property (nonatomic,weak)UITableView *tableView;
@property (nonatomic,weak)UIView *bottomView;
@property (nonatomic,strong)ZH_MallOrderViewModel *viewModel;
@property (nonatomic,strong)ZH_GoodsInfoView *infoView;
@property (nonatomic,strong)ZH_MallViewModel *viewModels;
@end

@implementation Zh_GoodsInformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUserInterface];
    //商品详情
    [self infoData];
}
- (void)cleanUpData{
    [self setViewModel:nil];
    [self setViewModels:nil];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.segmentView setSelectItem:0];
}
- (void)infoData{
    [self.viewModel.goodsDetailParam setObject:self.idString forKey:@"id"];
    WEAKSelf;
    [self.viewModel goodsDetailResultBlock:^(NSString *code, NSString *message, id data) {
        if ([code isEqualToString:@"0"]) {
            weakSelf.dic = data[@"data"];
            [weakSelf.tableView reloadData];
        }
        else{
            if ([code isEqualToString:@"1002"]) {
                [self showHint:@"登录过期"];
            }
            else{
                [weakSelf showHint:message];
            }
        }
        
    }];
}
- (void)initUserInterface{
    self.navigationItem.titleView = self.segmentView;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_spshare1"] style:UIBarButtonItemStylePlain target:self action:nil];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_bottom).offset(FITHEIGHT(-50));
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(FITHEIGHT(50));
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(self.bottomView.mas_top);
    }];
}
#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 2;
    }
    else if (section == 1){
        return 1;
    }
    else if (section == 2){
        return 1;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return FITSCALE(9);
    }
    else if (section == 2){
        return FITSCALE(9);
    }
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            GC_HPBannerTableViewCell *cell = [GC_HPBannerTableViewCell cellWithTableView:tableView];
            [cell setPicHeight:FITSCALE(232)];
            [cell updatesUIForImages:self.dic[@"goods"][@"goods_img"] andDefaultImage:@"img_banner"];
            return cell;
        }
        else if (indexPath.row == 1){
            ZH_GoodsInfoTableViewCell *cell = [ZH_GoodsInfoTableViewCell cellWithTableView:tableView];
            [cell updateUI:self.dic];
            cell.onButtonClick = ^{
                !self.infoView.isShow?[self.infoView showConditionChoseViewOnFather:self.view]:[self.infoView dissmissConditionChoseViewOnFather:self.view];
            };
            return cell;
        }
    }
    else if (indexPath.section == 1){
        ZH_GoodsCenterTableViewCell *cell = [ZH_GoodsCenterTableViewCell cellWithTableView:tableView];
        [cell updataUI:self.dic];
        return cell;
    }
    else if(indexPath.section == 2){
        ZH_GoodsInformationTableViewCell *cell = [ZH_GoodsInformationTableViewCell cellWithTableView:tableView];
        [cell updata:self.dic];
        return cell;
    }
    return nil;
}
#pragma mark - GC_GoodsSegmentDelegate
///当前选中项
- (void)customGoodsSegmentCurrentItem:(NSInteger)item
{
    //当前选项
    self.currentItem = item;
    if(item == 2){
        ZH_GoodsEvaluationViewController *vc = [[ZH_GoodsEvaluationViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        vc.idString = self.idString;
        [self.navigationController pushViewController:vc animated:YES];
    }
    //加载数据
    //    [self startLoadData];
}
#pragma mark - button
- (void)buttonClick:(UIButton *)btn{
    if(btn.tag == 3){
        if ([NSString stringWithFormat:@"%@",[self.viewModels.shopCarParam objectForKey:@"id"]].length > 0) {
            WEAKSelf;
            [self.viewModels shopCarResultBlock:^(NSString *code, NSString *message, id data) {
                if ([code isEqualToString:@"0"]) {
                    [weakSelf showHint:message];
                }
                else{
                    [weakSelf showHint:message];
                }
            }];
        }else{
            [self.viewModels.shopCarParam setObject:[NSString stringWithFormat:@"%@",self.dic[@"goods_detail"][0][@"id"]] forKey:@"id"];
            WEAKSelf;
            [self.viewModels shopCarResultBlock:^(NSString *code, NSString *message, id data) {
                if ([code isEqualToString:@"0"]) {
                    [weakSelf showHint:message];
                }
                else{
                    [weakSelf showHint:message];
                }
            }];
        }
    }
    
}
#pragma mark - getter,setter
///选择 View
- (GC_GoodsSegmentView *)segmentView
{
    if(!_segmentView){
        GC_GoodsSegmentView *view = [[GC_GoodsSegmentView alloc] initWithHeight:kNavBarHeight];
        //设置默认，选中字体
        [view setDefaultFont:kFontSize_36 selectFont:kFontSize_36];
        [view setDefaultFontColor:[UIColor colorWithHexString:@"#D9D9D9"] andSelectFontColor:kFontColorWhite];
        view.delegate = self;
        [view setUnLineItems:@[@"商品",@"详情",@"评价"]];
        [view setTopLineImageViewHidden:YES];
        [view setBottomLineImageViewHidden:YES];
        [view setSelectItem:0];
        [self.view addSubview:_segmentView = view];
    }
    return _segmentView;
}
- (UITableView *)tableView{
    if (!_tableView) {
        UITableView *tableView = [UITableView createTableViewWithDelegateTarget:self];
        tableView.backgroundColor = kCommonBackgroudColor;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [tableView setCellAutoAdaptationForEstimatedRowHeight:FITSCALE(100)];
        [self.view addSubview:_tableView = tableView];
    }
    return _tableView;
}
- (UIView *)bottomView{
    if (!_bottomView) {
        UIView *view = [UIView createViewWithBackgroundColor:kCommonWhiteBg];
        view.userInteractionEnabled = YES;
        [self.view addSubview:_bottomView = view];
        UIImageView *shareView = [UIImageView createImageViewWithName:@"icon_market"];
        [view addSubview:shareView];
        [shareView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(view).offset(FITHEIGHT(7));
            make.left.equalTo(view).offset(FITWITH(33));
            make.width.mas_equalTo(FITSCALE(22));
            make.height.mas_equalTo(FITSCALE(20));
        }];
        UILabel *shareLabel = [UILabel createLabelWithText:@"店铺" font:kFontSize_26];
        shareLabel.textColor = [UIColor colorWithHexString:@"#626262"];
        [view addSubview:shareLabel];
        [shareLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(shareView.mas_bottom).offset(FITHEIGHT(5));
            make.centerX.equalTo(shareView);
        }];
        ///line
        UIView *lineView = [UIView createViewWithBackgroundColor:[UIColor colorWithHexString:@"#E5E5E5"]];
        [view addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(shareView.mas_right).offset(FITWITH(24));
            make.top.bottom.equalTo(view);
            make.width.mas_equalTo(FITWITH(1));
        }];
        UIButton *shareButton = [UIButton createNoBgButtonWithTitle:@"" target:self action:@selector(buttonClick:)];
        shareButton.tag = 1;
        shareButton.backgroundColor = [UIColor clearColor];
        [view addSubview:shareButton];
        [shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.left.equalTo(view);
            make.right.equalTo(lineView.mas_right);
        }];
        UIImageView *collView = [UIImageView createImageViewWithName:@"icon_service"];
        [view addSubview:collView];
        [collView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(shareView);
            make.left.equalTo(lineView.mas_right).offset(FITWITH(25));
            make.width.mas_equalTo(FITSCALE(22));
            make.height.mas_equalTo(FITSCALE(20));
            
        }];
        UILabel *collLabel = [UILabel createLabelWithText:@"客服" font:kFontSize_26];
        collLabel.textColor = [UIColor colorWithHexString:@"#626262"];
        [view addSubview:collLabel];
        [collLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(shareView.mas_bottom).offset(FITHEIGHT(5));
            make.centerX.equalTo(collView);
        }];
        UIButton *collButton = [UIButton createNoBgButtonWithTitle:@"" target:self action:@selector(buttonClick:)];
        collButton.tag = 2;
        collButton.backgroundColor = [UIColor clearColor];
        [view addSubview:collButton];
        [collButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(view);
            make.left.equalTo(lineView.mas_right);
            make.right.equalTo(collView.mas_right).offset(FITWITH(24));
        }];
        
        UIButton *haveButton = [UIButton createNoBgButtonWithTitle:@"加入购物车" target:self action:@selector(buttonClick:)];
        haveButton.tag = 3;
        haveButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
        haveButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        haveButton.backgroundColor = [UIColor colorWithHexString:@"#CCEEEE"];
        haveButton.titleLabel.font = kFontSize_28;
        [haveButton setTitleColor:[UIColor colorWithHexString:@"#00C9AE"] forState:UIControlStateNormal];
        [view addSubview:haveButton];
        [haveButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(view);
            make.left.equalTo(collButton.mas_right);
            make.right.equalTo(view).offset(FITWITH(-130));
        }];
        UIButton *haveButtons = [UIButton createNoBgButtonWithTitle:@"立即购买" target:self action:@selector(buttonClick:)];
        haveButtons.tag = 4;
        haveButtons.backgroundColor = [UIColor colorWithHexString:@"#00C9AE"];
        haveButtons.titleLabel.font = kFontSize_28;
        [haveButtons setTitleColor:kFontColorWhite forState:UIControlStateNormal];
        [view addSubview:haveButtons];
        [haveButtons mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(view);
            make.left.equalTo(haveButton.mas_right);
            make.right.equalTo(view);
        }];
        
    }
    return _bottomView;
}
- (ZH_MallOrderViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[ZH_MallOrderViewModel alloc] init];
    }
    return _viewModel;
}
- (ZH_GoodsInfoView *)infoView{
    if (!_infoView) {
        _infoView = [[ZH_GoodsInfoView alloc] init];
        [_infoView update:self.dic[@"spec"]];
        WEAKSelf;
        
        _infoView.onButtonClick = ^(NSString *num, NSString *titleString) {
            if (num.length > 0) {
                if (titleString.length > 9) {
                    [weakSelf.viewModels.shopCarParam setObject:num forKey:@"num"];
                    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0];
                    [array addObjectsFromArray:weakSelf.dic[@"goods_detail"]];
                    for (NSDictionary *dic in array) {
                        if ([titleString isEqualToString:dic[@"spec_detail"]]) {
                            [weakSelf.viewModels.shopCarParam setObject:[NSString stringWithFormat:@"%@",dic[@"id"]] forKey:@"id"];
                        }
                    }
                    
                }
                else{
                    [weakSelf showHint:@"请选择商品相应的规格"];
                }
            }
            else{
                [weakSelf.viewModels.shopCarParam setObject:@"1" forKey:@"num"];
            }
            
        };
    }
    return _infoView;
}
- (ZH_MallViewModel *)viewModels{
    if (!_viewModels) {
        _viewModels = [[ZH_MallViewModel alloc] init];
    }
    return _viewModels;
}
@end
